package cn.crm.system.service.impl;

import cn.crm.common.annotation.DataScope;
import cn.crm.common.utils.SecurityUtils;
import cn.crm.system.domain.pojo.dto.*;
import cn.crm.system.domain.pojo.entity.Business;
import cn.crm.system.domain.pojo.entity.Series;
import cn.crm.system.domain.pojo.vo.*;
import cn.crm.system.mapper.BusinessMapper;
import cn.crm.system.mapper.ContactsMapper;
import cn.crm.system.mapper.CrmpictureMapper;
import cn.crm.system.mapper.SeriesMapper;
import cn.crm.system.service.ISeriesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 处理表头的接口
 */

@Slf4j
@Service
public class SeriesServiceImpl implements ISeriesService {
    @Autowired
    private SeriesMapper seriesMapper;
    @Autowired
    private CrmpictureMapper crmpictureMapper;

//增加
    @Override
    public Long insertSeries(SeriesDto seriesDto) {
        log.debug("开始出来增加表单的业务层");
        Series series = new Series();
        BeanUtils.copyProperties(seriesDto,series);
        String createAccount = SecurityUtils.getUsername();
        series.setCreateBy(createAccount);
        Long Long = seriesMapper.insertSeries(series);
        Long seriesId = series.getId();
        if (seriesId != null) {
            List<Crmpicture> crmpictureList = seriesDto.getCrmpicture();
            crmpictureList.forEach(crmpicture -> {
                crmpicture.setSeriesId(seriesId);
                crmpicture.setCreateBy(createAccount);
            });
            crmpictureMapper.insertCrmpicture(crmpictureList);
        }

        return seriesId;
    }
//查表头全部信息
    @Override
    public List<SeriesVo> SelectSeries() {
        log.debug("开始查询出表头信息");
        List<SeriesVo> seriesVoList = seriesMapper.SelectSeries();
        return seriesVoList;
    }
    //查询出系列和型号
    @Override
    public List<SeriesAndspecificationVo> SelectSeriesspecification(SeriesDto seriesDto) {
        log.debug("查询出表头和表单的共同信息");
        List<SeriesAndspecificationVo> seriesAndspecificationVoList = seriesMapper.SelectSeriesspecification(seriesDto);
        return seriesAndspecificationVoList;
    }
//删除
    @Override
    public int delSeriesspecification(Long[] id,  Long[] crmpictureId) {
        log.debug("开始删除表单数据");
        //删除图片数据
        int j=0;
        int i =0;
        int k =0;
        //先通过id查看看有几笔有这个id的数据
        int row = crmpictureMapper.selectcrmpicturebyidInt(id);
        //如果就一笔就删除图片数据和表头
        if (row ==1 || row ==0){
            j = crmpictureMapper.delCrmpicture(crmpictureId);
            //删除表头数据
             i = seriesMapper.delSeriesspecification(id);
             //删除表身子数据
            k= seriesMapper.delSpecification(id);

        }else {
            //否则只删除图片的数据
             j = crmpictureMapper.delCrmpicture(crmpictureId);
            int rows = crmpictureMapper.selectcrmpicturebyidInt(id);
            if (rows==0){
                //删除表头数据
                i = seriesMapper.delSeriesspecification(id);
                //删除表身子数据
                k= seriesMapper.delSpecification(id);
            }
        }

        return  i+j+k;
    }
    //通过id查图片和表头
    @Override
    public SeriesVo selectseriesbyid(Long id) {
        //查询出表头的
        SeriesVo seriesVo = seriesMapper.selectseriesbyid(id);
        //查询图片的
        List<CrmpictureVo> crmpictureList = crmpictureMapper.selectcrmpicturebyid(id);
        seriesVo.setCrmpictureVos(crmpictureList);
        return  seriesVo;
    }
//修改
        @Override
    public int updateSeries(SeriesDto seriesDto) {
        log.debug("开始修改表头数据");
        int i = seriesMapper.updateSeries(seriesDto);
        int i1=0;
        //修改图片
        List<Crmpicture> crmpicture = seriesDto.getCrmpicture();
        //通过crmpicture中的id来修改
        List<Long> collect = crmpicture.stream().map(Crmpicture::getId).collect(Collectors.toList());
        if (collect.size() > 0){
            i1 = crmpictureMapper.updateCrmpicture(crmpicture);
        }
        if (i+i1<1){
            return 0;
        }
        return i+i1;
    }
//轮播图
    @Override
    public List<SeriesVo> Selectbanner() {
        log.debug("获取轮播图");
        List<SeriesVo> seriesVos = seriesMapper.Selectbanner();
        return seriesVos;
    }
//查出全部前台用
    @Override
    public List<SeriesAndadll> selectall() {
        //先查出来全部系列
        List<SeriesAndadll> seriesAndadllList = seriesMapper.selectall();
      //在通过查出来的系统的id查询出对应的规格
        for (SeriesAndadll seriesAndadll : seriesAndadllList){
            List<ProdectVo> prodectVos = seriesMapper.selectProdect(seriesAndadll.getId());
            seriesAndadll.setProdectVos(prodectVos);
        }
        return seriesAndadllList;
    }
//通过id查出全部
    @Override
    public SeriesAndadll selectallById(Long id) {
        SeriesAndadll seriesAndadllList = seriesMapper.selectallById(id);
        if (seriesAndadllList != null){
            List<ProdectVo> prodectVos = seriesMapper.selectProdect(id);
            seriesAndadllList.setProdectVos(prodectVos);
            return seriesAndadllList;
        }
        return seriesAndadllList;
    }

}
